package com.ernest.controller;

import com.ernest.service.ILoginInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ernest
 * @date 2022年05月05
 */
@Controller
@RequestMapping("/")
public class LoginJsonController {

    @Autowired
    private ILoginInfoService loginInfoService;

    @PostMapping("/loginajax")
    @ResponseBody
    public Map<String, String> parseLoginAjax(@RequestBody Map<String, String> params){
        HashMap<String, String> resp = new HashMap<>();

        String account = params.get("account");
        String pwd = params.get("pwd");
        String result = loginInfoService.loginInfo(account, pwd);
        if ("success".equals(result)) {
            // 登录成功
            resp.put("status", "success");
        }else{
            // 登录失败
            resp.put("status", "failed");
            if ("hasExit".equals(result)) {
                resp.put("msg", "用户不存在");
            }else{
                resp.put("msg", "密码错误");
            }
        }
        return resp;
    }
}
